package dao;

import model.Customer;
import model.Supplier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_customer extends Dao{

    //获取所有客户信息
    public static List selectCus()
    {
        List list = new ArrayList();
        String sql = "select * from client";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Customer customer = new Customer();

               customer.setCusID(re.getString("cl_no"));
               customer.setCusName(re.getString("cl_name"));
               customer.setCusPhone(re.getString("cl_phone"));
               customer.setCusAddress(re.getString("cl_address"));

                list.add(customer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //添加客户
    public static int addCus(String cl_no, String cl_name, String cl_phone, String cl_address)
    {
        int i = 0;
        try {
            String sql = "insert into client(cl_no, cl_name, cl_phone, cl_address) values('"+ cl_no +"','"+ cl_name +"','"+ cl_phone + "','" + cl_address + "')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //获取客户id
    public static Vector<String> getCusID()
    {
        List list = Dao_customer.selectCus();
        Vector<String> customer = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Customer customers = (Customer) list.get(i);
            customer.add(customers.getCusID());
        }

        return customer;
    }

    //删除客户
    public static int deleteCus(String cusID)
    {
        int i = 0;

        try {
            String sql = "delete from  client where cl_no = '" + cusID + "'";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //修改客户信息
    public static int updateCus(String cl_no, String cl_name, String phone, String address)
    {
        int i = 0;

        try {
            String sql = "update  client set cl_no = '" + cl_no + "', cl_name = '" + cl_name + "', cl_phone = '" + phone + "', cl_address = '"+ address + "' where cl_no = '" + cl_no + "'";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //获取客户名称
    public static Vector<String> getCustomer()
    {
        List list = Dao_customer.selectCus();
        Vector<String> customer = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Customer customers = (Customer) list.get(i);
            customer.add(customers.getCusName());
        }

        return customer;
    }

    //通过客户名称获取客户信息
    public static List selectAll_customer(String customerName)
    {
        List list = new ArrayList();
        String sql = "select * from client where cl_name ='" +  customerName +"'";

        ResultSet re = Dao.executeQuery(sql);
        try {
            while (re.next())
            {
                Customer customer = new Customer();

                customer.setCusID(re.getString("cl_no"));
                customer.setCusPhone(re.getString("cl_phone"));
                customer.setCusName(re.getString("cl_name"));
                customer.setCusAddress(re.getString("cl_address"));

                list.add(customer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}

